package com.ruoyi.web.controller.base;

import com.ruoyi.base.domain.BaseLimit;
import com.ruoyi.base.service.IBaseLimitService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.YesNoStatus;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统配置Controller
 *
 * @author zzp
 * @date 2022-09-28
 */
@RestController
@RequestMapping("/base/limit")
public class BaseLimitController extends BaseController {
    @Autowired
    private IBaseLimitService baseLimitService;

    /**
     * 查询系统配置列表
     */
    @PreAuthorize("@ss.hasPermi('base:limit:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseLimit baseLimit) {
        startPage();
        List<BaseLimit> list = baseLimitService.selectBaseLimitList(baseLimit);
        return getDataTable(list);
    }

    /**
     * 导出系统配置列表
     */
    @PreAuthorize("@ss.hasPermi('base:limit:export')")
    @Log(title = "系统配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseLimit baseLimit) {
        List<BaseLimit> list = baseLimitService.selectBaseLimitList(baseLimit);
        ExcelUtil<BaseLimit> util = new ExcelUtil<BaseLimit>(BaseLimit.class);
        util.exportExcel(response, list, "系统配置数据");
    }

    /**
     * 获取系统配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:limit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(baseLimitService.selectBaseLimitById(id));
    }

    /**
     * 新增系统配置
     */
    @PreAuthorize("@ss.hasPermi('base:limit:add')")
    @Log(title = "系统配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseLimit baseLimit) {
        BaseLimit limit = baseLimitService.selectBaseLimitUse();
        if (StringUtils.isNotNull(limit)) {
            return AjaxResult.error("存在已经启用的系统配置，不可添加");
        }
        return toAjax(baseLimitService.insertBaseLimit(baseLimit));
    }

    /**
     * 修改系统配置
     */
    @PreAuthorize("@ss.hasPermi('base:limit:edit')")
    @Log(title = "系统配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseLimit baseLimit) {
        BaseLimit limit = baseLimitService.selectBaseLimitById(baseLimit.getId());
        if (!limit.getStatus().equals(baseLimit.getStatus())) {
            if (baseLimit.getStatus().equals(YesNoStatus.YES.getCode())) {
                BaseLimit limitUse = baseLimitService.selectBaseLimitUse();
                if (StringUtils.isNotNull(limitUse)) {
                    return AjaxResult.error("存在已经启用的系统配置，不可添加");
                }
            }
        }
        return toAjax(baseLimitService.updateBaseLimit(baseLimit));
    }

    /**
     * 删除系统配置
     */
    @PreAuthorize("@ss.hasPermi('base:limit:remove')")
    @Log(title = "系统配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        BaseLimit limit = baseLimitService.selectBaseLimitUse();
        if (StringUtils.isNotNull(limit)) {
            return AjaxResult.error("存在已经启用的系统配置，不可删除");
        }
        return toAjax(baseLimitService.deleteBaseLimitByIds(ids));
    }
}
